<template>
  <div class="h-panel">
    <div class="h-panel-bar">
      <span class="h-panel-title">应用管理</span>
    </div>
    <div class="h-panel-body">
      <div class="h-panel-body-top">
        <input type="text" v-model="menu_keyword" placeholder="请输入名称查询"/>
        <Select placeholder="请选择应用状态" v-model="menu_status" autosize :datas="menu_status_lsit"></Select>
        <Button class="h-btn h-btn-s">查询</Button>
        <Button class="h-btn h-btn-s">新增</Button>
      </div>
      <Table ref="table" :datas="data" border>
        <TableItem title="微博ID" prop="id" :width="120"></TableItem>
        <TableItem title="微博标题" prop="weibo_title" :width="220"></TableItem>
        <TableItem title="微博内容">
          <div class="weibo_content" slot-scope="{data}">
            <p v-html="filterHTMLTag(data.weibo_content)"></p>
          </div>
        </TableItem>
        <TableItem title="发布时间" prop="weibo_date" :width="160"></TableItem>
        <TableItem title="发布状态" :width="80">
          <ButtonGroup slot-scope="{data}">
             <span class="h-tag h-tag-bg-primary" v-if="data.weibo_is_send">发布成功</span>
             <span class="h-tag h-tag-bg-red" v-if="!data.weibo_is_send">发布失败</span>
          </ButtonGroup>
        </TableItem>
        <TableItem title="微博地址" :width="80">
          <ButtonGroup slot-scope="{data}">
            <Button size="s" class="h-btn h-btn-blue" @click="goWeiboUrl(data)" :disabled="!data.weibo_url">微博地址</Button>
          </ButtonGroup>
        </TableItem>
        <TableItem title="操作" :width="60">
          <ButtonGroup slot-scope="{data}">
            <Button size="s" @click="subWeibo(data)" :disabled="data.weibo_is_send">发布</Button>
            <!-- <Button size="s" icon="h-icon-plus" @click="remove(data)">下载授权文件</Button> -->
            <!-- <Button size="s" icon="h-icon-trash" color="red" @click="remove(datas, data)">删除授权</Button> -->
          </ButtonGroup>
        </TableItem>
      </Table>
      <div class="h-panel-body-bottom">
        <Pagination v-model="pagination" small align="right" @change="currentChange"></Pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      pagination: {
        page: 1,
        size: 10,
        total: 100
      },
      menu_keyword: '',
      menu_status: null,
      menu_status_lsit: { 0: '发布失败', 1: '发布成功' },
      data: []
    }
  },
  mounted () {
    // this.$refs.table.setRowSelect(this.datas[0])
  },
  created () {
    this.getList()
  },
  methods: {
    filterHTMLTag (data) {
      var msg = data.replace(/<\/?[^>]*>/g, '')
      msg = msg.replace(/[|]*\n/, '')
      msg = msg.replace(/&npsp;/ig, '')
      return msg
    },
    subWeibo (weibo) {
      console.log(weibo)
      return new Promise((resolve, reject) => {
        this.$Axios({ url: 'weibo/sub', method: 'post', data: {weibo_id: weibo.id} })
          .then(resp => {
            // 请求成功
            if (resp.code === 1) {
              this.data = resp.data
            } else {
              this.$Message.error(resp.msg)
            }
            resolve(resp)
          })
          .catch(error => {
            // 请求失败
            this.$_requestInterceptor('error', error)
            reject(error)
          })
      })
    },
    goWeiboUrl (data) {
      if (data.weibo_url) {
        window.open(data.weibo_url)
      }
    },
    getList () {
      return new Promise((resolve, reject) => {
        this.$Axios({ url: 'weibo/list', method: 'get', data: {pagesize: 10, page: 0} })
          .then(resp => {
            // 请求成功
            if (resp.code === 1) {
              this.data = resp.data
            }
            resolve(resp)
          })
          .catch(error => {
            // 请求失败
            this.$_requestInterceptor('error', error)
            reject(error)
          })
      })
    },
    currentChange (e) {
      console.log(this.pagination)
    }
  }
}
</script>
